package org.school.streaming.net.client;

/**
 * @authors Jeffrey Goudzwaard,
 * 			Davin Terrizzi
 * @version	1.0
 * @date	18-4-2013
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.school.streaming.net.State;
import org.school.streaming.net.client.listeners.btnPauseListener;
import org.school.streaming.net.client.listeners.btnPlayListener;
import org.school.streaming.net.client.listeners.btnSetupListener;
import org.school.streaming.net.client.listeners.btnTeardownListener;

/**
 * Class representing GUI for Client
 * @author Davin
 *
 */
public class ClientFrame extends JFrame {
	private static final long serialVersionUID = 2199803193288863953L;

	private static final String NAME = "Client";

	private JPanel pnlMain, pnlButtons;

	private JButton btnSetup, btnPlay, btnPause, btnTeardown;

	private static JLabel lblIcon;

	private Client client;

	/**
	 * Constructor
	 * Initializes GUI
	 */
	public ClientFrame() {
		init();
	}

	/**
	 * Initializes GUI
	 */
	private void init() {
		// Setting the title
		setTitle(NAME);

		// Creating the buttons
		btnSetup = new JButton();
		btnSetup.setText("Setup");
		btnSetup.addActionListener(new btnSetupListener());

		btnPlay = new JButton();
		btnPlay.setText("Play");
		btnPlay.addActionListener(new btnPlayListener());

		btnPause = new JButton();
		btnPause.setText("Pause");
		btnPause.addActionListener(new btnPauseListener());

		btnTeardown = new JButton();
		btnTeardown.setText("Teardown");
		btnTeardown.addActionListener(new btnTeardownListener());

		// Creating the labels
		lblIcon = new JLabel();
		lblIcon.setText("");
		lblIcon.setIcon(null);
		lblIcon.setBounds(0, 0, 380, 280);

		// Creating the panels
		pnlButtons = new JPanel();
		pnlButtons.setLayout(new GridLayout(1, 0));
		pnlButtons.setBounds(0, 280, 380, 50);
		pnlButtons.add(btnSetup);
		pnlButtons.add(btnPlay);
		pnlButtons.add(btnPause);
		pnlButtons.add(btnTeardown);

		pnlMain = new JPanel();
		pnlMain.setLayout(null);
		pnlMain.add(lblIcon);
		pnlMain.add(pnlButtons);

		// Creating the event handlers
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// Tell VM to close without any errors
				System.exit(0);
			}
		});

		// Adding the main panel to the frame
		getContentPane().add(pnlMain, BorderLayout.CENTER);

		// Setting the size
		setSize(new Dimension(390, 370));

		// Displaying the frame
		setVisible(true);
	}

	/**
	 * Updates video frame in client GUI
	 * @param image New image frame
	 */
	public static void setIcon(Image image) {
		ImageIcon icon = new ImageIcon(image);
		lblIcon.setIcon(icon);
	}

	/*
	 * Initializes Client instance and some needed properties
	 * Sets server listening port + server host address + server IP address for Client
	 */
	public void start(int serverPort, String host, InetAddress serverIP)
			throws Exception {
		client = Client.getClient();

		client.setHost(host);

		client.setSocket(new Socket(serverIP, serverPort));

		client.setReader(new BufferedReader(new InputStreamReader(client
				.getSocket().getInputStream())));
		client.setWriter(new BufferedWriter(new OutputStreamWriter(client
				.getSocket().getOutputStream())));

		client.setState(State.INIT);
	}
}
